package com.framework.loippi.controller.admin;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.SystemMessage;
import com.framework.loippi.mybatis.paginator.domain.Order.Direction;
import com.framework.loippi.service.SystemMessageService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Controller - 消息表
 *
 * @author wmj
 * @version 2.0
 */
@Controller("adminSystemMessageController")
@RequestMapping({"/admin/system_message"})
public class SystemMessageController extends GenericController {

    @Resource
    private SystemMessageService systemMessageService;

    /**
     * 跳转添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = {"/add"}, method = {RequestMethod.GET})
    public String add(ModelMap model) {
        return "/admin/system_message/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(SystemMessage systemMessage, String userIds, RedirectAttributes redirectAttributes) {
        systemMessage.setCreateTime(new Date());
        systemMessage.setPushTime(new Date());
        systemMessageService.save(systemMessage);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, ModelMap model) {
        SystemMessage systemMessage = systemMessageService.find(id);
        model.addAttribute("systemMessage", systemMessage);
        return "/admin/system_message/edit";
    }


    /**
     * 详情
     */
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, ModelMap model) {
        SystemMessage systemMessage = systemMessageService.find(id);
        model.addAttribute("systemMessage", systemMessage);
        return "/admin/system_message/view";
    }


    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(SystemMessage systemMessage, RedirectAttributes redirectAttributes) {
        systemMessageService.update(systemMessage);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 列表查询
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public String list(Pageable pageable, HttpServletRequest request, ModelMap model) {

        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        Map map = new HashMap();
        for (String key : paramter.keySet()) {
            if (!StringUtil.isEmpty(paramter.get(key).toString())) {
                map.put(key, paramter.get(key));
            }
        }
        pageable.setParameter(map);
        pageable.setOrderDirection(Direction.DESC);
        pageable.setOrderProperty("ID");
        model.addAttribute("paramter", paramter);
        model.addAttribute("page", this.systemMessageService.findByPage(pageable));
        return "/admin/system_message/list";
    }

    /**
     * 删除操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    public @ResponseBody
    Message delete(Long[] ids) {
        this.systemMessageService.deleteAll(ids);
        return SUCCESS_MESSAGE;
    }

    /**
     * 列表查询
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = {"/listUser"}, method = {RequestMethod.POST})
    @ResponseBody
    public String user(HttpServletRequest request, Integer pageNumber, String filter_phone) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", filter_phone);
        map.put("isEnable", 1);//0-禁用 1-启用
        Pageable pageable = new Pageable();
        pageable.setPageNumber(pageNumber);
        pageable.setPageSize(15);
        pageable.setParameter(map);
        return ApiUtils.success();
    }
}
